﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Media;
using ThePresenter.Net.PresenterCom.Uids;
using ThePresenter.Windows;
using ThePresenter.Windows.Controls;


namespace ThePresenter.Net.PresenterCom.Actions
{
	[Serializable]
	class NewSlideAction : PresentationAction, ISerializable
	{
		#region Fields
		Uid _SlideID;

		Size _SlideDimensions;

		byte[] _SlideBackground;

		StrokeCollection _SlideStrokes;
		#endregion

		public NewSlideAction(Slide Source)
		{
			_SlideID = Source.Uid;
			_SlideStrokes = (StrokeCollection)Source.Canvas.Dispatcher.Invoke(new Func<StrokeCollection>(() => Source.Canvas.Strokes.Clone()));
			_SlideBackground = (byte[])Source.Canvas.Dispatcher.Invoke(new Func<byte[]>(() => Source.Canvas.GetBackground())); ;
			_SlideDimensions = Source.Size;
		}

		public override void Execute(Presentation Target)
		{
			Slide S = new Slide();
			S.Uid = _SlideID;
			S.Size = _SlideDimensions;

			S.Canvas = new InkCanvas();
			S.Canvas.Tag = _SlideStrokes.GetUidedStrokeCollection();
			S.Canvas.Strokes = _SlideStrokes;
			S.Canvas.SetBackground(_SlideBackground);

			Target.AddSlide(S);
		}

		#region ISerializable Members
		public NewSlideAction(SerializationInfo info, StreamingContext context)
		{
			_SlideID = (Uid)info.GetValue("Guid", typeof(Uid));
			_SlideDimensions = (Size)info.GetValue("CanvasSize", typeof(Size));
			_SlideBackground = (byte[])info.GetValue("Background", typeof(byte[]));
			_SlideStrokes = ((byte[])info.GetValue("Strokes", typeof(byte[]))).ToStrokes();
		}

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Guid", _SlideID, typeof(Uid));
			info.AddValue("CanvasSize", _SlideDimensions, typeof(Size));
			info.AddValue("Background", _SlideBackground, typeof(byte[]));
			info.AddValue("Strokes", _SlideStrokes.ToBytes(), typeof(byte[]));
		}
		#endregion
	}
}
